package com.music.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.dao.UserFocusMapper;
import com.music.dao.UserMapper;
import com.music.dao.UserVipMapper;
import com.music.entity.ObjVo;
import com.music.entity.User;
import com.music.entity.UserVip;
import com.music.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @className: UserServiceImpl
 * @description:
 * @author: 林鑫海
 * @date: 2022/7/4
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserVipMapper userVipMapper;
    @Autowired
    private UserFocusMapper userFocusMapper;
    @Override
    public boolean registerUser(String userPhone, String userPassword){
        User user = new User();
        //用户id
        user.setUserId(UUID.randomUUID().toString().replace("-",""));
        //默认初始注册昵称=手机号
        user.setUserName(userPhone);
        //手机号
        user.setUserPhone(userPhone);
        //密码
        user.setUserPassword(userPassword);
        //默认初始注册状态-正常
        user.setUserStatus("1");
        int i = userMapper.insertSelective(user);
        return i>0;
    }

    @Override
    public boolean existOfPhoneNumber(String userPhone) {
       User user = new User();
       user.setUserPhone(userPhone);
        List<User> userPhoneList = userMapper.findSelective(user);
        return !userPhoneList.isEmpty();
    }

    @Override
    public User login(String userPhone, String userPassword) {
        User user = new User();
        user.setUserPhone(userPhone);
        user.setUserPassword(userPassword);
        List<User> list = userMapper.findSelective(user);
        if (!list.isEmpty()){
            //获取登录用户
            User loginUser = list.get(0);
            //登录成功，将用户对象存入session中
            request.getSession().setAttribute("user",loginUser);
            return loginUser;
        }
        return null;
    }

    @Override
    public void logout() {
        request.getSession().removeAttribute("user");
    }

    @Override
    public User getUserBySession() {
        return (User) request.getSession().getAttribute("user");
    }

    /**
     * 肖泽强
     * @param map 多条件
     * @return
     */
    @Override
    public PageInfo<User> getUserByConditions(Map<String, Object> map) {
        if(map.get("userId")!= null && !"".equals(map.get("userId"))){
            map.put("userId",new StringBuffer().append("%").append(map.get("userId")).append("%").toString());
        }
        if(map.get("userName")!= null && !"".equals(map.get("userName"))){
            map.put("userName",new StringBuffer().append("%").append(map.get("userName")).append("%").toString());
        }
        if(map.get("userPhone")!= null && !"".equals(map.get("userPhone"))){
            map.put("userPhone",new StringBuffer().append("%").append(map.get("userPhone")).append("%").toString());
        }
        if(map.get("userStatus")!= null && !"".equals(map.get("userStatus"))){
            map.put("userStatus",new StringBuffer().append("%").append(map.get("userStatus")).append("%").toString());
        }
        PageHelper.startPage(Integer.valueOf(map.get("pageNum").toString()),Integer.valueOf(map.get("pageSize").toString()));
        List<User> userList = userMapper.findUserByConditions(map);
        Optional.ofNullable(userList).ifPresent(users ->{
            users.forEach(user ->{
                switch (user.getUserStatus()){
                    case "0":user.setUserStatus("冻结");
                        break;
                    case "1":user.setUserStatus("正常");
                        break;
                    default:
                }

            });
        });
        PageInfo<User> PageInfo = new PageInfo<>(userList);
        return PageInfo;
    }

    @Override
    public boolean updateUserByKey(User user) {

        return userMapper.updateUserByKey(user) > 0;
    }

    @Override
    public User findByUserId(String userId) {
        return userMapper.findByUserId(userId);
    }

    @Override
    public List<ObjVo> findUserInformationById(String userId) {
        List<ObjVo> list=new ArrayList<ObjVo>();
        ObjVo u1=new ObjVo();
        u1.setObjName("userName");
        u1.setObjValue(userMapper.findByUserId(userId).getUserName());
        ObjVo u2=new ObjVo();
        u2.setObjName("userImg");
        u2.setObjValue(userMapper.findByUserId(userId).getUserImage());
        ObjVo u3=new ObjVo();
        u3.setObjName("focus");
        u3.setObjValue(String.valueOf(userFocusMapper.focusNum(userId)));
        ObjVo u4=new ObjVo();
        u4.setObjName("fans");
        u4.setObjValue(String.valueOf(userFocusMapper.fansNum(userId)));
        ObjVo u5=new ObjVo();
        u5.setObjName("Vip");
        UserVip userVip=userVipMapper.fingById(userId);
        if(userVip==null){
            u5.setObjValue("2");
        }else{
            u5.setObjValue(String.valueOf(userVipMapper.fingById(userId).getUserVipStatus()));
        }

        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        return list;
    }
}
